package com.motionlog.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.motionlog.R;
import com.motionlog.adapter.MyBaseAdapter;
import com.motionlog.model.Base;
import com.motionlog.model.Database;
import com.motionlog.model.Method;
import com.motionlog.model.Reason;

public class SelectMethod extends Activity implements OnClickListener {

	private static final String TAG = SelectMethod.class.getSimpleName();

	List<Base> motions = new ArrayList<Base>();
	MyBaseAdapter adapter;
	ListView list;

	Database mDb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_method);

		mDb = Database.getInstance(this);

		Button back = (Button) findViewById(R.id.back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		ImageButton add = (ImageButton) findViewById(R.id.new_item);
		add.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				AlertDialog.Builder builder = new Builder(SelectMethod.this);
				builder.setTitle("输入新的应对方式");
				View view = SelectMethod.this.getLayoutInflater().inflate(
						R.layout.view_new_motion, null);
				TextView desc = (TextView) view.findViewById(R.id.desc);
				desc.setVisibility(View.GONE);
				final EditText text = (EditText) view.findViewById(R.id.text);
				builder.setView(view);
				builder.setNegativeButton("取消", null);
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Method m = new Method();
								m.name = text.getText().toString();
								m.id = (int) mDb.insertMotionLike("method",
										m.name);
								android.util.Log.d(TAG, "m.id: " + m.id
										+ ", m.name: " + m.name);
								motions.add(0, m);
								adapter.notifyDataSetChanged();
							}
						});
				builder.show();
			}
		});

		list = (ListView) findViewById(R.id.list);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		motions.addAll(Database.getInstance(this).getMotions(Method.class));
		adapter = new MyBaseAdapter(this, motions, true);
		list.setAdapter(adapter);

		Button confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(this);
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			SparseBooleanArray ids = list.getCheckedItemPositions();
			StringBuilder summary = new StringBuilder();
			ArrayList<Base> methods = new ArrayList<Base>();
			for (int i = 0; i < ids.size(); i++) {
				if (ids.valueAt(i)) {
					android.util.Log.d(TAG, "id: " + ids.keyAt(i));
					Base m = adapter.getItem((int) ids.keyAt(i));
					summary.append(m.name);
					summary.append(',');
					methods.add(m);
				}
			}
			Intent i = new Intent();
			i.putExtra("summary", summary.toString());
			i.putParcelableArrayListExtra("methods", methods);
			setResult(RESULT_OK, i);
			finishActivity(0);
			finish();
			break;
		case R.id.cancel:
			list.clearChoices();
			adapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
	}
}
